﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Xml;
using System.Xml.Xsl;
using Viewer.Base.Config;
using System.Data;
using System.Xml.Linq;
using System.IO;
using Enterprise.Web;

namespace Viewer.Base
{
    public partial class SimpleListBase : BaseUserControl
    {

        #region properties

        #endregion

        public void SetRowCommandWithUser(string pUserID)
        {
            CommandColumn _commandColumn = new CommandColumn { Width = 40 };
            GridCommand _gridCommand = new GridCommand { Icon = Icon.ArrowDown };
                
            // btnAddNew.Visible = false;
            foreach (var item in this.GetGridCommandWithUser(pUserID))
            {
                //if (item.Type.Value.ToString().Equals((int)Config.ViewerConstant.GridCommandType.Add))
                //{
                //    btnAddNew.Visible = true;
                //}
                //else
                //{
                //    _gridCommand.Menu.Items.Add(new Ext.Net.MenuCommand { Text = item.Name.Value.ToString(), Icon = (Icon)Enum.Parse(typeof(Icon), item.Icon.Value.ToString()), CommandName=item.Type.Value.ToString()});
                //}
            }
            _commandColumn.Commands.Add(_gridCommand);
            GrdDetails.ColumnModel.Columns.Add(_commandColumn);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnAddNew.DirectClick += new ComponentDirectEvent.DirectEventHandler(btnAddNew_DirectClick);
        }

        private void btnAddNew_DirectClick(object sender, DirectEventArgs e)
        {
            ((BusinessPage)(this.Page)).AddEvent();
        }

        [DirectMethod]
        public void ShowDetail(int id)
        {
           // ((BusinessPage)(this.Page)).show
        }

        public void SetDataSource(IEnumerable<object> pDataSource)
        {
            this.StoMainSource.DataSource = pDataSource.ToList();
            this.StoMainSource.DataBind();
        }

        public void SetAutoExpandColumn(string pColumnName)
        {
            //must specify columnID.
            GrdDetails.AutoExpandColumn = "col" + pColumnName;
        }

        public void AddStoreColumn(string pFieldName, RecordFieldType pFieldType)
        {
            RecordField recordField;
            switch (pFieldType)
            {
                case RecordFieldType.String:
                    recordField = new RecordField(pFieldName);
                    break;
                case RecordFieldType.Boolean:
                case RecordFieldType.Int:
                case RecordFieldType.Float:
                    recordField = new RecordField(pFieldName, pFieldType);
                    break;
                case RecordFieldType.Date:
                    recordField = new RecordField(pFieldName, pFieldType);
                    break;
                default:
                    recordField = new RecordField();
                    break;
            }
            if (X.IsAjaxRequest)
            {
                this.StoMainSource.AddField(recordField, false);
            }
            else
            {
                this.StoMainSource.Reader.Reader.Fields.Add(recordField);
            }
        }

        public void AddGridColumn(string pIndexName, string pHeaderName)
        {
            //columnID will automatically create with prefix "col" with HeaderName
            this.GrdDetails.ColumnModel.Columns.Add(new Column { ColumnID = "col" + pHeaderName, DataIndex = pIndexName, Header = pHeaderName });
        }

        public void AddGridColumn(string pIndexName, string pHeaderName, int pColumnWidth)
        {
            this.GrdDetails.ColumnModel.Columns.Add(new Column { ColumnID = "col" + pIndexName, DataIndex = pIndexName, Header = pHeaderName, Width = pColumnWidth });
        }

        public void AddGridDateColumn(string pIndexName, string pHeaderName, string pDateFormat)
        {
            this.GrdDetails.ColumnModel.Columns.Add(new DateColumn { ColumnID = "col" + pHeaderName, DataIndex = pIndexName, Header = pHeaderName, Format = pDateFormat });
        }

        public void AddGridDateColumn(string pIndexName, string pHeaderName, string pDateFormat, int pColumnWidth)
        {
            this.GrdDetails.ColumnModel.Columns.Add(new DateColumn { ColumnID = "col" + pHeaderName, DataIndex = pIndexName, Header = pHeaderName, Format = pDateFormat, Width = pColumnWidth });
        }
        public void ReconfigureGrid()
        {
            // chỉ thực hiện ReconfigGrid trong trường hợp thay đổi layout column với Ajax Request.
            if (X.IsAjaxRequest)
            {
                this.GrdDetails.Reconfigure();

                //trong truong hop command column thi moi chay cau lenh duoi'
                this.GrdDetails.RegisterColumnPlugins();
            }
        }

        public void ClearMetaDataDataSource()
        {
            this.StoMainSource.ClearMeta();
        }

        public void SetGridTitle(string pGridTitle)
        {
            GrdDetails.Title = pGridTitle;
        }



        public void SetPageSize(int pPageSzie)
        {
            PagingToolbarList.PageSize = pPageSzie;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.GrdDetails.ColumnModel.RegisterCommandStyleRules();
            }
        }

        protected override void StoMainSource_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            Ext.Net.X.AddScript("Ext.net.Mask.hide();");
            string format = this.FormatType.Value.ToString();
            string strDownloadFileName = GrdDetails.Title;
            XmlNode xml = e.Xml;
            this.Export(format, strDownloadFileName, e);
        }


        //internal void SetDataSource(List<object> nsToChucs)
        //{
        //    this.StoMainSource.DataSource = nsToChucs;
        //    this.StoMainSource.DataBind();
        //}
    }
}